<?php

/**
 * @author 	Samuel Carlier
 * @package	P3Base_Mailer
 */
class P3Base_Mailer_Message {
	
	/**
	 * @var P3Base_Mailer_MessagePart
	 */
	private $_text = null;

	/**
	 * @var P3Base_Mailer_MessagePart
	 */
	private $_html = null;
	
	/**
	 * @var string
	 */
	private $_subject;

	/**
	 * @var string
	 */
	private $_charset;
	
	/**
	 * @var string
	 */
	const ENCODING_7BIT = '7bit';
	
	/**
	 * @var string
	 */
	const ENCODING_8BIT = '8bit';
	
	/**
	 * @var string
	 */
	const ENCODING_QUOTE = 'quoted-printable';

	/**
	 * constructor
	 */
	public function __construct() {
		$this->_charset 	= 'ISO-8859-1';
	}

	/**
	 * @param P3Base_Mailer_MessagePart $text
	 */
	public function setText(P3Base_Mailer_MessagePart $text) {
		$this->_text = $text;	
	}
	
	/**
 	 * @param P3Base_Mailer_MessagePart $html
 	 */
	public function setHTML(P3Base_Mailer_MessagePart $html) {
		$this->_html = $html;	
	}
	
	/**
	 * @return bool
	 */
	public function hasText() {
		return $this->_text instanceof P3Base_Mailer_MessagePart;
	}
		
	/**
	 * @return bool
	 */
	public function hasHTML() {
		return $this->_html instanceof P3Base_Mailer_MessagePart;
	}
	
	/**
	 * @return P3Base_Mailer_MessagePart
	 */
	public function getText() {
		return $this->_text;	
	}
	
	/**
	 * @return P3Base_Mailer_MessagePart
	 */
	public function getHTML() {
		return $this->_html;	
	}

	/**
	 * @return string
	 */
	public function getSubject() {
		return $this->_subject;	
	}
	
	/**
	 * @return string $subject
	 */
	public function setSubject($subject) {
		$this->_subject = $subject;	
	}

	/**
	 * @param string $charset
	 */
	public function setCharset($charset) {
		$this->_charset = $charset;	
	}

	/**
	 * @return string
	 */
	public function getCharset() {
		return $this->_charset;	
	}

	/**
	 * @return string
	 */
	public function getMailMessage() {
		$header  = 'Content-Type:%s;' . P3Base_Mailer_Handler_SMTP::CRLF;
		$header .= "\t" . 'charset=' . $this->_charset .  P3Base_Mailer_Handler_SMTP::CRLF;
		$header .= 'Content-Transfer-Encoding:%s' . P3Base_Mailer_Handler_SMTP::CRLF;
		$header .= P3Base_Mailer_Handler_SMTP::CRLF;
		$header .= '%s';

		$html = is_null($this->_html) ? null : sprintf($header, 'text/html' , $this->_html->getEncoding(),
																			  $this->_html->getContent());

		$text = is_null($this->_text) ? null : sprintf($header, 'text/plain', $this->_text->getEncoding(),
																			  $this->_text->getContent());

		/* multipart/alternative */
		if(!is_null($this->_html) && !is_null($this->_text)) {
			$boundary = md5(uniqid());
			
			$message  = 'Content-Type:multipart/alternative;' . P3Base_Mailer_Handler_SMTP::CRLF;
			$message .= "\t" . 'boundary="' . $boundary .'"' . P3Base_Mailer_Handler_SMTP::CRLF;

			$message .= P3Base_Mailer_Handler_SMTP::CRLF;
			
			$message .= '--' . $boundary . P3Base_Mailer_Handler_SMTP::CRLF;
			$message .= $text . P3Base_Mailer_Handler_SMTP::CRLF;

			$message .= '--' . $boundary . P3Base_Mailer_Handler_SMTP::CRLF;
			$message .= $html . P3Base_Mailer_Handler_SMTP::CRLF;

			/* close multipart/alternative */
			$message .= '--' . $boundary . '--';

		} else {
			$message = is_null($html) ? $text : $html;
		}

		return $message;
	}
}

?>
